package cn.zwxky.android.staff.Follow;

import android.content.Context;
import android.net.Uri;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.config.Config;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.DensityUtils;
import com.zgscwjm.lsfbbasetemplate.utils.SN;
import com.zgscwjm.lsfbbasetemplate.utils.T;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.zwxky.android.staff.Login.ForgetBean;
import cn.zwxky.android.staff.R;
import cn.zwxky.android.staff.Utils.ListViewForScrollView;
import cn.zwxky.android.staff.Utils.LittleUtils;
import cn.zwxky.android.staff.Utils.NoDoubleClickUtils;
import cn.zwxky.android.staff.Utils.URLString;

/**
 * 领导普通任务-任务详情
 * Created by Administrator on 2017/3/29 0029.
 */
@ContentView(R.layout.aty_follow)
public class ArrangeFollowActivity2 extends LsfbActivity {

    /**
     * 头像
     */
    @ViewInject(R.id.fg_my_img_head)
    private SimpleDraweeView img_head;

    /**
     * 名字
     */
    @ViewInject(R.id.aty_follow_text_name)
    private TextView text_name;

    /**
     * 时间
     */
    @ViewInject(R.id.aty_follow_text_time)
    private TextView text_time;

    /**
     * 内容
     */
    @ViewInject(R.id.aty_follow_text_countext)
    private TextView text_countext;

    @ViewInject(R.id.aty_follow_et_import)
    private EditText et_import;


//    /**
//     * 点击刷新布局控件
//     */
//    @ViewInject(R.id.swiplayout)
//    private SwipeRefreshLayout swipeRefreshLayout;
//
//    /**
//     * list列表
//     */
//    @ViewInject(R.id.dish_list)
//    private RecyclerView dish_list;

    @ViewInject(R.id.dish_list)
    private ListViewForScrollView dish_list;

//    /**
//     * RecyclerView的布局管理器
//     */
//    private RecyclerView.LayoutManager mlayoutManager;

    private FollowAdapter adapter;

    private List<FollowBean.ListBean> mList;

    private String id = "";

    private String status;

    @Override
    public void initView() {
        getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        LittleUtils.setsimpletitlebar(this, "项目跟进");
        TextView textView = new TextView(this);
        textView.setText("完成");
        textView.setTextColor(getResources().getColor(R.color.white_color));
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (LittleUtils.job_yw.equals("1") && status.equals("2")){
                    SN.SnackShow(dish_list, "您已提交完成，不能重复提交");
                    return;
                }
                if (LittleUtils.job_yw.equals("0") && status.equals("1")){
                    SN.SnackShow(dish_list, "您已提交完成，不能重复提交");
                    return;
                }
                if (!NoDoubleClickUtils.isDoubleClick()) {
                    taskOver();
                }


            }
        });
        getTitleBar().setRightView(textView, 0, 0, DensityUtils.dp2px(this, 10), 0);

        id = getIntent().getStringExtra("id");

        mList = new ArrayList<>();
        adapter = new FollowAdapter(this, R.layout.item_follow, mList);
//        mlayoutManager = new LinearLayoutManager(this);//布局模式为listview方式
//        dish_list.setLayoutManager(mlayoutManager);
        dish_list.setAdapter(adapter);

//        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                getData();
//            }
//        });

    }

    @Override
    public void initEvent() {

    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.aty_follow_btn_post})
    public void onClick(View view) {
        switch (view.getId()) {
            //发送
            case R.id.aty_follow_btn_post:
                if (!NoDoubleClickUtils.isDoubleClick()) {
                    send();
                }

                break;
        }
    }

    @Override
    public void initData() {
        getData();
    }

    public void getData() {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("uid", Config.getUserId());
        hashMap.put("id", id);

        new BaseInternet().getData_Get(URLString.TASKXQ, hashMap, FollowBean.class, new ArrangeFollowEvent<FollowBean>(), true,true);
    }

    @EventAnnotation
    public void FollowEvent(ArrangeFollowEvent<FollowBean> event) {
//        swipeRefreshLayout.setRefreshing(false);
        switch (event.getCode()) {
            case 200:
                if (!event.getData().getImg().isEmpty()) {
                    img_head.setImageURI(Uri.parse(URLString.IMG_URL + event.getData().getImg()));
                } else {
                    img_head.setBackgroundResource(R.mipmap.img_my_head);
                }
                status=event.getData().getStatus();
                text_name.setText(event.getData().getName());
                text_time.setText(event.getData().getTimes());
                text_countext.setText(event.getData().getContent());

                mList.clear();
                mList.addAll(event.getData().getList());
                adapter.notifyDataSetChanged();
                break;
            default:
                SN.SnackShow(dish_list, event.getMsg());
                break;
        }
    }

    public void send() {

        if (et_import.getText().toString().isEmpty()) {
            SN.SnackShow(dish_list, "请输入内容");
            return;
        }
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("uid", Config.getUserId());
        hashMap.put("id", id);
        hashMap.put("content", et_import.getText().toString());
        new BaseInternet().getData(URLString.TASKTALK, hashMap, ForgetBean.class, new ArrangeSendEvent<ForgetBean>(), false);
    }

    @EventAnnotation
    public void SendEvent(ArrangeSendEvent<ForgetBean> event) {
        switch (event.getCode()) {
            case 200:
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(et_import.getWindowToken(), 0) ;
                SN.SnackShow(dish_list, event.getMsg());
                et_import.setText("");
                getData();
                break;
            default:
                SN.SnackShow(dish_list, event.getMsg());
                break;
        }
    }

    /**
     * 完成
     */
    public void taskOver() {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("uid", Config.getUserId());
        hashMap.put("id", id);

        new BaseInternet().getData(URLString.NORMAL_TASK_OVER, hashMap, ForgetBean.class, new ArrangeTaskOverEvent<ForgetBean>(), false);
    }

    @EventAnnotation
    public void TaskOverEvent(ArrangeTaskOverEvent<ForgetBean> event) {
        switch (event.getCode()) {
            case 200:
                T.showShort(this,event.getMsg());
                finish();
                break;
            default:
                SN.SnackShow(dish_list, event.getMsg());
                break;
        }
    }

}
